package com.appengine.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.datatypes.Activity;
import com.datatypes.ActivityTracks;
import com.datatypes.Friend;
import com.datatypes.FriendsZone;
import com.datatypes.OfflineLogs;
import com.datatypes.Settings;
import com.datatypes.Track;
import com.datatypes.User;
import com.datatypes.UserTracks;

public class SavingAndLoadingCenter {

	public static User loadUser(String id, Context c) {
		FileInputStream fis;
		User user = null;
		try {
			fis = c.openFileInput(id);
			ObjectInputStream ois = new ObjectInputStream(fis);
			String usrStr = (String) ois.readObject();
			user = GsonUtils.ConvertGsonStringToUser(usrStr);
			ois.close();
			fis.close();

		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
		}

		return user;
	}

	public static void saveUser(User user, Context c) {
		FileOutputStream fos;
		try {
			fos = c.openFileOutput(user.GetFacebookID(), Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(GsonUtils.ConvertUserToGsonString(user));
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static UserTracks loadUserTracks(String id, Context c) {
		FileInputStream fis;
		UserTracks userTracks = null;
		try {
			fis = c.openFileInput(id + "USERTRACKS");
			ObjectInputStream ois = new ObjectInputStream(fis);
			String usrStr = (String) ois.readObject();
			userTracks = GsonUtils.ConvertGsonStringToUserTracks(usrStr);
			ois.close();
			fis.close();

		} catch (IOException e) {
			e.printStackTrace();
			if (userTracks == null)
				userTracks = new UserTracks();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (userTracks == null)
				userTracks = new UserTracks();
		}

		return userTracks;
	}

	public static void saveUserTracks(String id, UserTracks userTracks,
			Context c) {
		FileOutputStream fos;
		try {
			fos = c.openFileOutput(id + "USERTRACKS", Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(GsonUtils.ConvertUserTracksToGsonString(userTracks));
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void saveUserTracks(String id, List<Track> listOfTracks,
			Context c) {

		UserTracks oldUT = loadUserTracks(id, c);

		if (oldUT != null) {
			for (Track t : listOfTracks)
				if (oldUT.getTrack(t.getDate()) == null)
					oldUT.addTrack(t, null);
		} else
			oldUT = new UserTracks(listOfTracks);

		Comparator<Track> comparator = new Comparator<Track>() {
			@Override
			public int compare(Track lhs, Track rhs) {
				return (int) (lhs.getDate() - rhs.getDate());
			}

		};
		Collections.sort(oldUT.getTracksList(), comparator);
		saveUserTracks(id, oldUT, c);
	}

	public static ActivityTracks loadActivityTracks(String id, Context c) {
		FileInputStream fis;
		ActivityTracks activityTracks = null;
		try {
			fis = c.openFileInput(id + "ACTIVITYTRACKS");
			ObjectInputStream ois = new ObjectInputStream(fis);
			String usrStr = (String) ois.readObject();
			activityTracks = GsonUtils
					.ConvertGsonStringToActivityTracks(usrStr);
			ois.close();
			fis.close();

		} catch (IOException e) {
			e.printStackTrace();
			if (activityTracks == null)
				activityTracks = new ActivityTracks();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (activityTracks == null)
				activityTracks = new ActivityTracks();
		}

		return activityTracks;
	}

	public static void saveActivityTracks(String id,
			ActivityTracks activityTracks, Context c) {
		FileOutputStream fos;
		try {
			fos = c.openFileOutput(id + "ACTIVITYTRACKS", Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(GsonUtils
					.ConvertActivityTracksToGsonString(activityTracks));
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void saveActivityTracks(String id,
			List<Activity> listOfActivity, Context c) {

		ActivityTracks oldAT = new ActivityTracks(listOfActivity);
		saveActivityTracks(id, oldAT, c);
	}

	public static FriendsZone loadFriendsZone(String id, Context c) {
		FileInputStream fis;
		FriendsZone activityTracks = null;
		try {
			fis = c.openFileInput(id + "FRIENSZONE");
			ObjectInputStream ois = new ObjectInputStream(fis);
			String fzStr = (String) ois.readObject();
			activityTracks = GsonUtils.ConvertGsonStringToFriendsZone(fzStr);
			ois.close();
			fis.close();

		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
		}

		return activityTracks;
	}

	public static void saveFriendsZone(String id, FriendsZone fz, Context c) {
		FileOutputStream fos;
		try {
			fos = c.openFileOutput(id + "FRIENSZONE", Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(GsonUtils.ConvertFriendsZoneToGsonString(fz));
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void saveFriendImage(String id, Bitmap image, Context c) {
		FileOutputStream fos;

		try {
			fos = c.openFileOutput(id + "_FRIENDIMAGE", Context.MODE_PRIVATE);
			image.compress(Bitmap.CompressFormat.PNG, 100, fos);
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static Bitmap loadFriendImage(String id, Context c) {
		FileInputStream fis;
		Bitmap friendImg = null;
		try {
			fis = c.openFileInput(id + "_FRIENDIMAGE");
			friendImg = BitmapFactory.decodeStream(fis);
			fis.close();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		}

		return friendImg;
	}

	public static void deleteFriendsImages(List<Friend> friends, Context c) {
		if (friends != null)
			for (Friend f : friends) {
				c.deleteFile(f.getFbID() + "_FRIENDIMAGE");
			}
	}

	public static void deleteMyImage(String id, Context c) {
		c.deleteFile(id + "_FRIENDIMAGE");
	}

	public static OfflineLogs loadOfflineLogs(String id, Context c) {
		FileInputStream fis;
		OfflineLogs offlinelogs = null;
		try {
			fis = c.openFileInput(id + "LOGS");
			ObjectInputStream ois = new ObjectInputStream(fis);
			String oll = (String) ois.readObject();
			offlinelogs = GsonUtils.ConvertGsonStringToOfflineLogs(oll);
			ois.close();
			fis.close();

		} catch (IOException e) {
			offlinelogs = new OfflineLogs();
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (offlinelogs == null)
				offlinelogs = new OfflineLogs();

		}

		return offlinelogs;
	}

	public static void saveOfflineLogs(String id, OfflineLogs oll, Context c) {
		FileOutputStream fos;
		try {
			fos = c.openFileOutput(id + "LOGS", Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(GsonUtils.ConvertOfflineLogsToGsonString(oll));
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void deleteLogs(String id, Context c) {
		c.deleteFile(id + "LOGS");
	}

	public static Settings loadSettings(Context c) {
		FileInputStream fis;
		Settings s = null;
		try {
			fis = c.openFileInput("SETTINGS");
			ObjectInputStream ois = new ObjectInputStream(fis);
			String settings = (String) ois.readObject();
			s = GsonUtils.ConvertGsonStringToSettings(settings);
			ois.close();
			fis.close();

		} catch (IOException e) {
			if (s == null)
				s = new Settings();
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			if (s == null)
				s = new Settings();
			e.printStackTrace();
		} finally {
			if (s == null)
				s = new Settings();
		}

		return s;
	}

	public static void saveSettings(Settings s, Context c) {
		FileOutputStream fos;
		try {
			fos = c.openFileOutput("SETTINGS", Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(GsonUtils.ConvertSettingsToGsonString(s));
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String loadFB_ID(Context c) {
		FileInputStream fis;
		String s = null;
		try {
			fis = c.openFileInput("FBID");
			ObjectInputStream ois = new ObjectInputStream(fis);
			String str = (String) ois.readObject();
			s = str;
			ois.close();
			fis.close();

		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
		}

		return s;
	}

	public static void saveFB_ID(Context c, String str) {
		FileOutputStream fos;
		try {
			fos = c.openFileOutput("FBID", Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(str);
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void deleteFB_ID(Context c) {
		c.deleteFile("FBID");
	}

	
	
	public static String loadNetworkState(Context c) {
		FileInputStream fis;
		String s = null;
		try {
			fis = c.openFileInput("NETWORKSTATE");
			ObjectInputStream ois = new ObjectInputStream(fis);
			String str = (String) ois.readObject();
			s = str;
			ois.close();
			fis.close();

		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
		}

		return s;
	}

	public static void saveNetworkState(Context c, String str) {
		FileOutputStream fos;
		try {
			fos = c.openFileOutput("NETWORKSTATE", Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(str);
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void deleteNetworkState(Context c) {
		c.deleteFile("NETWORKSTATE");
	}
	
	
	
	
	
	
	public static long loadLastUpdatedTrack(Context c) {
		FileInputStream fis;
		String s = null;
		long lastUpdatedTrack = 0;
		try {
			fis = c.openFileInput("LASTTRACK");
			ObjectInputStream ois = new ObjectInputStream(fis);
			String str = (String) ois.readObject();
			if (str != null) {
				lastUpdatedTrack = Long.parseLong(str);
			}
			ois.close();
			fis.close();

		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
		}

		return lastUpdatedTrack;
	}

	public static void saveLastUpdatedTrack(Context c, long track_date) {
		FileOutputStream fos;
		try {
			fos = c.openFileOutput("LASTTRACK", Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject("" + track_date);
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void deleteLastUpdatedTrack(Context c) {
		c.deleteFile("LASTTRACK");
	}

	public static void deleteAll(String id, Context c) {
		c.deleteFile("LASTTRACK");
		c.deleteFile(id + "FRIENSZONE");
		c.deleteFile((id + "USERTRACKS"));
		c.deleteFile(id + "ACTIVITYTRACKS");
		c.deleteFile(id + "ACTIVITYTRACKS");
		c.deleteFile("FBID");
		c.deleteFile(id);

	}

}
